package com.treasure.mall.zbqmallmanager.controller.auction;

import com.treasure.mall.biz.dto.auction.AuctionSaveDTO;
import com.treasure.mall.biz.dto.condition.AuctionSessionCondition;
import com.treasure.mall.biz.dto.condition.BizOrderCondition;
import com.treasure.mall.biz.entity.auction.AuctionBidPO;
import com.treasure.mall.biz.entity.auction.AuctionSessionPO;
import com.treasure.mall.biz.entity.order.BizOrderPO;
import com.treasure.mall.biz.service.core.auction.AuctionBidService;
import com.treasure.mall.biz.service.core.auction.AuctionSessionService;
import com.treasure.mall.biz.service.core.order.BizOrderService;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.constant.Constant;
import com.treasure.mall.zbq.common.ienum.order.OrderEnums;
import com.treasure.mall.zbqmallmanager.vo.AuctionSessionBaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/7/16
 */
@Controller
@RequestMapping("/auction")
public class AuctionController {
    @Autowired
    private AuctionSessionService sessionService;
    @Autowired
    private AuctionBidService auctionBidService;
    @Autowired
    private BizOrderService bizOrderService;

    @GetMapping("/list")
    public String list(
            @ModelAttribute("condition") AuctionSessionCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<AuctionSessionPO> sessionPOS = sessionService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", sessionPOS.getTotalElements());
        model.addAttribute("totalPages", sessionPOS.getTotalPages());
        model.addAttribute("list", sessionPOS.getContent());

        return "auction/auction_session_list";
    }

    @PostMapping("/edit")
    @ResponseBody
    public ApiResult create(AuctionSaveDTO auctionSaveDTO) {
        sessionService.save(auctionSaveDTO);

        return ApiResult.ok();
    }

    @GetMapping("/currentSession")
    @ResponseBody
    public ApiResult currentSession(Long productId) {
        AuctionSessionPO sessionPO = sessionService.currentSession(productId);

        return ApiResult.ok(new AuctionSessionBaseVO(sessionPO));
    }

    @GetMapping("/bidList")
    public String bidList(
            @RequestParam(required = false, defaultValue = "0") long productId,
            @RequestParam(required = false, defaultValue = "0") long sessionId,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        if (sessionId == 0) {
            AuctionSessionPO sessionPO = sessionService.currentSession(productId);
            if (sessionPO != null) {
                sessionId = sessionPO.getId();
            }
        }

        Page<AuctionBidPO> bidPOS = auctionBidService.findBySessionId(sessionId, pageIndex, Constant.DEFAULT_PAGE_SIZE);

        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", bidPOS.getTotalElements());
        model.addAttribute("totalPages", bidPOS.getTotalPages());
        model.addAttribute("list", bidPOS.getContent());
        model.addAttribute("productId", productId);
        model.addAttribute("sessionId", sessionId);

        return "auction/auction_bid_list";
    }

    /**
     * 拍卖金订单列表
     *
     * @param condition
     * @param pageIndex
     * @param model
     * @return
     */
    @GetMapping("/biz/list")
    public String bizOrders(
            @ModelAttribute("condition") BizOrderCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        condition.setDealType(OrderEnums.DealType.AUCTION.getCode());
        Page<BizOrderPO> orderPOS = bizOrderService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);

        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", orderPOS.getTotalElements());
        model.addAttribute("totalPages", orderPOS.getTotalPages());
        model.addAttribute("orders", orderPOS.getContent());

        return "auction/security_eposit_list";
    }

    /**
     * 发起退款
     *
     * @return
     */
    @PostMapping("/refund")
    @ResponseBody
    public ApiResult refund(String bizOrderId) {
        BizOrderPO po = bizOrderService.findById(bizOrderId);
        bizOrderService.refund(po);
        return ApiResult.ok();
    }
}
